<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>学生信息表</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="/js/bootstrap.min.js"></script> <!-- 添加 Bootstrap 的 JavaScript 文件 -->
</head>
<body>
<div class="container-fluid">
    学生信息
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" style="margin: 20px" data-toggle="modal" data-target="#myModal">
        新增学生
    </button>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>学号</th>
            <th>年龄</th>
            <th>性别</th>
            <th>成绩</th>
            <th>密码</th>
            <th style="width: 100px;">操作一</th>
            <th style="width: 100px;">操作二</th>
        </tr>
        </thead>
        <tbody id="studentTb">
        </tbody>
    </table>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="fromStudent">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="name">
                    </div>
                    <div class="form-group">
                        <label for="num">学号</label>
                        <input type="text" class="form-control" id="num" name="num" placeholder="num">
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="age">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password"
                               placeholder="Password">
                    </div>
                </form>
                <!--                表单结束-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="addStudent();">新增</button>
                <button type="button" class="btn btn-primary" onclick="updateStudent();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
    // 手动初始化 Bootstrap modal 插件
    function editStudent(id) {
        $.ajax({
            url: "/webapi/student/get/" + id,
            method: "GET",
        }).done(function (rs) {
            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#num").val(rs.num);
            $("#age").val(rs.age);
            $("#sex").val(rs.sex);
            $("#score").val(rs.score);
            $("#password").val(rs.password)
            $("#myModal").modal('show');
        })
    }

    //更新學生信息
    // 更新学生信息
    function updateStudent() {
        var updatedStudent = {
            id: $("#id").val(),
            name: $("#name").val(), // 获取学生姓名
            num: $("#num").val(),//获取学生学号
            age: $("#age").val(), // 获取学生年龄
            sex: $("#sex").val(), // 获取学生性别
            score: $("#score").val(), // 获取学生成绩
            password: $("#password").val(), // 获取学生密码
        };
        $.ajax({
            url: "/webapi/student/update/",
            type: "PUT",
            data: JSON.stringify(updatedStudent),
            contentType: "application/json", // 设置Content-Type头部
        }).done(function () {
            $("#myModal").modal('hide'); // 隐藏模态框
            loadTable(); // 更新表格数据
            window.alert("更新成功！");
        });
    }

    // 增加学生
    function addStudent() {
        var data = $("#fromStudent").serialize();
        $.ajax({
            url: "/webapi/student/insert",
            method: "post",
            data: data
        }).done(function () {
            loadTable();
            window.alert("添加成功")
            $("#myModal").modal('hide');
        })
    }

    // 删除学生
    function delStudent(id) {
        var msg = window.confirm("是否刪除")
        console.log(msg)
        if (msg == true) {
            $.ajax({
                url:"/webapi/student/delete/"+id,
                method:"delete"
            }).done(function (){
                loadTable();
                window.alert("刪除成功")
            })
        } else {
             window.alert("取消刪除")
        }
    }


    function loadTable() {
        $('#myModal').modal('hide');
        $.ajax({
            url: "/webapi/student/list"
        }).done(function (rs) {
            var len = rs.length;
            var html = "";
            for (var i = 0; i < len; i++) {
                var item = rs[i];
                html += "<tr>"
                    + "<td>" + item.id + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.num + "</td>"
                    + "<td>" + item.age + "</td>"
                    + "<td>" + item.sex + "</td>"
                    + "<td>" + item.score + "</td>"
                    + "<td>" + item.password + "</td>"
                    + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a></td>"
                    + "<td><a href='#' onclick='delStudent(" + item.id + ");'>刪除</a></td>"
                    + "</tr>"
            }
            $("#studentTb").html(html);
        })
    }

    $(function () {
        loadTable();
    });
</script>
</body>
</html>